package com.augurit.gzcgwsp.sendNotice.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.gzcgwsp.sendNotice.entity.YwWebserviceInfo;

@Repository
public class YwWebserviceInfoDao  extends BaseDao<YwWebserviceInfo, Long>{

	/**
	 * 保存webservice接口的信息
	 * @param ywWebserviceInfo
	 */
	public void save(YwWebserviceInfo ywWebserviceInfo){
		getSession().saveOrUpdate(ywWebserviceInfo);
	}
	
	/**
	 * 获得还没有发送的记录  案件状态接口
	 * @return
	 */
	public List getNoticeYwWebserviceInfo(){
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
		Date date = new Date();
//		date.setDate(date.getDate()+1);
		String hql = "from YwWebserviceInfo wb where wb.paraThree='feedBackApprovalInfo' and wb.isSended is null" +
//				" and ((wb.callTimes<=50 and to_char(wb.callDate,'yyyyMMdd') != '"+sf.format(date)+"') or (wb.callTimes<50 and to_char(wb.callDate,'yyyyMMdd') = '"+sf.format(date)+"')) " +
				" and (wb.callTimes<50 or (wb.callTimes>=50 and to_char(wb.callDate,'yyyyMMdd') != '"+sf.format(date)+"'))" +
				" order by wb.produceDate asc"; 
		Query query = this.getSession().createQuery(hql);
		List list = query.list();
		return list;
	}
	
	/**
	 * 获得还没有发送的同步用户或者行政组织记录
	 * @return
	 */
	public List getsyncUserAndOrgYwWebServiceInfo(){
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
		Date date = new Date();
		date.setDate(date.getDate()+1);
		String hql = "from YwWebserviceInfo wb where (wb.paraThree='newUser'" +
				" or wb.paraThree='modifyUser'" +
				" or wb.paraThree='modifyUserPassword'" +
				" or wb.paraThree='disuseUser'" +
				" or wb.paraThree='modifyUserOrg'" +
				" or wb.paraThree='newOrganization'" +
				" or wb.paraThree='modifyOrganization'" +
				" or wb.paraThree='disUseOrganization') and wb.isSended is null " +
				" and (wb.callTimes<50 or (wb.callTimes>=50 and to_char(wb.callDate,'yyyyMMdd') != '"+sf.format(date)+"'))" +
				" order by wb.produceDate asc";
		Query query = this.getSession().createQuery(hql);
		List list = query.list();
		return list;
	}
	
	/**
	 * 获得还没有发送的记录  公示结束接口
	 * @return
	 */
	public List getStopPublicYwWebserviceInfo(){
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
		Date date = new Date();
		date.setDate(date.getDate()+1);
		String hql = "from YwWebserviceInfo wb where wb.paraThree='stopPublicApprovalInfo' and wb.isSended is null" +
		" and (wb.callTimes<50 or (wb.callTimes>=50 and to_char(wb.callDate,'yyyyMMdd') != '"+sf.format(date)+"'))" +
		" order by wb.produceDate asc";
		Query query = this.getSession().createQuery(hql);
		List list = query.list();
		return list;
	}
	
	/**
	 * 当webservice调用成功，则把改记录的isSended字段的值改成1
	 * @param id
	 */
	public void changState(long id){
		String hql = "update YwWebserviceInfo wb set wb.isSended='1' where wb.id=:id";
		Query query = this.getSession().createQuery(hql);
		query.setLong("id", id);
		query.executeUpdate();
	}
	
	/**
	 * 把改记录的isSended字段的值改成空，则定时器可以查到
	 * @param acceptNo系统受理号
	 * @param approvalNo方欣那边的受理号
	 */
	public void changStateToNullByAcceptNo(String acceptNo,String approvalNo){
		String hql = "update YwWebserviceInfo wb set wb.paraFour='"+approvalNo+"',wb.isSended=null where wb.acceptNo=:acceptNo and wb.paraThree='feedBackApprovalInfo'";
		Query query = this.getSession().createQuery(hql);
		query.setString("acceptNo", acceptNo);
		query.executeUpdate();
	}
	/**
	 * 根据acceptNo获取结束公示的一条记录
	 * @param acceptNo
	 * @return
	 */
	public YwWebserviceInfo getStopYwWebserviceInfoByAcceptNo(String acceptNo){
		String hql = "from YwWebserviceInfo wb where wb.paraThree='stopPublicApprovalInfo' and wb.acceptNo='"+acceptNo+"' and wb.isSended is null";
		Query query = this.getSession().createQuery(hql);
		List list = query.list();
		if(list != null && list.size() > 0){
			return (YwWebserviceInfo)list.get(0);
		}
		return null;
	}
}
